package com.hd.crdao.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.adapter.DomainHisAdapter;
import com.hd.crdao.adapter.DomainNaneAdapter;
import com.hd.crdao.base.BaseActivity;
import com.hd.crdao.bean.DomainNameBean;
import com.hd.crdao.bean.MyDomainBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 *
 *@author Administrator
 *@date 2017/7/4 0004
 */
public class SearchDomainActivity extends BaseActivity implements TextWatcher {
    @BindView(R.id.back)
    ImageButton mBack;
    @BindView(R.id.et_search)
    EditText mEtSearch;            //搜索的文本框
    @BindView(R.id.search)
    TextView mSearch;           //搜索按钮
    @BindView(R.id.lv_now)
    ListView mLvNow;
    @BindView(R.id.clear)
    TextView mClear;
    @BindView(R.id.listview_his)
    ListView mListviewHis;
    public String mBusinessid;
    public DomainNaneAdapter mDomainNaneAdapter;
    @BindView(R.id.success)
    LinearLayout mSuccess;
    @BindView(R.id.domian)
    TextView mDomian;
    public View mSuccess02;   //查询到不符合规则域名显示的视图
    public View mSuccess03;        //查询到被人占用显示的视图
    @BindView(R.id.ll_search)
    LinearLayout mLlSearch;
    @BindView(R.id.domian03)
    TextView mDomian03;
    public DomainHisAdapter mDomainHisAdapter;
    public String[] mSplit;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_domain;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        mSuccess02 = findViewById(R.id.success02);
        mSuccess03 = findViewById(R.id.success03);
    }

    @Override
    protected void initListener() {
        mEtSearch.addTextChangedListener(this);
        mLvNow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mDomainNaneAdapter != null) {
                    DomainNameBean.RowsBean item = (DomainNameBean.RowsBean) mDomainNaneAdapter.getItem(position);
                    Intent intent = new Intent(UIUtils.getContext(), BuyDomainActivity.class);
                    intent.putExtra("domainName", item.getDomainName());
                    intent.putExtra("domainPrice", item.getDomainPrice());
                    intent.putExtra("id", item.getId());
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void initData() {
        mBusinessid = SPUtils.getString("businessid", "");

        String hisdomain = SPUtils.getString("hisdomain", "");
        ALog.d(hisdomain);
        if(!TextUtils.isEmpty(hisdomain)){
            mSplit = hisdomain.split(",");
            mDomainHisAdapter = new DomainHisAdapter(mSplit);
            mListviewHis.setAdapter(mDomainHisAdapter);
        }
    }


    @OnClick({R.id.back, R.id.search, R.id.clear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.search:
                String domainName = mEtSearch.getText().toString().trim();
                if (TextUtils.isEmpty(domainName)) {
                    MyToast.show("请输入要搜索的域名");
                } else {
                    searchDomain(domainName);
                    String oldText = SPUtils.getString("hisdomain", "");
                    if (!TextUtils.isEmpty(domainName) && !oldText.contains(domainName + ",")) {
                        SPUtils.saveString("hisdomain", domainName + "," + oldText);
                    }

                    String hisdomain = SPUtils.getString("hisdomain", "");
                    ALog.d(hisdomain);
                    if(!TextUtils.isEmpty(hisdomain)){
                        mSplit = hisdomain.split(",");
                        mDomainHisAdapter = new DomainHisAdapter(mSplit);
                        mListviewHis.setAdapter(mDomainHisAdapter);
                    }
                }
                break;
            case R.id.clear:
                    SPUtils.saveString("hisdomain","");
                    if(mDomainHisAdapter!= null){
                        mSplit = null;
                        mDomainHisAdapter = null;
                        mListviewHis.setAdapter(mDomainHisAdapter);
                    }
                break;
        }
    }

    /**
     * 根据edittext的内容精确查找域名
     *
     * @param domainName
     */
    private void searchDomain(final String domainName) {
        try {
            OkHttpUtils.post().url(Constants.DOMIAN_SEARCH_URL).addParams("carBusinessId", mBusinessid)
                    .addParams("sign", Constants.SIGN).addParams("domainName", domainName).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    MyToast.show("数据加载失败,请检查网络");
                }

                @Override
                public void onResponse(String response, int id) {
                    ALog.d(response);
                    Gson gson = new Gson();
                    MyDomainBean myDomainBean = gson.fromJson(response, MyDomainBean.class);
                    String success = myDomainBean.getSuccess();       //请求成功的成功码
                    String message = myDomainBean.getMessage();
                    if (TextUtils.equals("00", success)) {
                        MyDomainBean.DomainNamesBean domainNames = myDomainBean.getDomainNames();
                        Intent intent = new Intent(UIUtils.getContext(), BuyDomainActivity.class);
                        intent.putExtra("domainName", domainNames.getDomainName());
                        intent.putExtra("domainPrice", domainNames.getDomainPrice());
                        intent.putExtra("id", domainNames.getId());
                        startActivity(intent);
                    } else if (TextUtils.equals("01", success)) {
                        MyToast.show(message);
                    } else if (TextUtils.equals("02", success)) {
                        mSuccess.setVisibility(View.GONE);
                        mSuccess02.setVisibility(View.VISIBLE);
                        mSuccess03.setVisibility(View.GONE);
                        mDomian.setText(domainName+".crdcar.com");

                    } else if (TextUtils.equals("03", success)) {
                        mSuccess.setVisibility(View.GONE);
                        mSuccess02.setVisibility(View.GONE);
                        mSuccess03.setVisibility(View.VISIBLE);
                        mDomian03.setText(domainName+".crdcar.com");
                    }

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 从服务器获取数据加载
     *
     * @param domainName
     */
    private void loadData(String domainName) {
        try {
            OkHttpUtils.post().url(Constants.DOMAIN_NAME_LIST_URL).addParams("pageNo", "1").addParams("pageSize", "10")
                    .addParams("carBusinessId", mBusinessid).addParams("sign", Constants.SIGN)
                    .addParams("domainName", domainName).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    MyToast.show("数据加载失败,请检查网络");
                }

                @Override
                public void onResponse(String response, int id) {
                    ALog.d(response);
                    Gson gson = new Gson();
                    DomainNameBean domainNameBean = gson.fromJson(response, DomainNameBean.class);
                    String success = domainNameBean.getSuccess();       //请求成功的成功码
                    String message = domainNameBean.getMessage();
                    ALog.d("成功码success=" + success);
                    if (TextUtils.equals("00", success)) {
                        List<DomainNameBean.RowsBean> rows = domainNameBean.getRows();
                        mDomainNaneAdapter = new DomainNaneAdapter(rows);
                        mLvNow.setAdapter(mDomainNaneAdapter);
                    } else {
                        MyToast.show(message);
                    }

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(s.length()==15){
            MyToast.show("域名最多只能15位");
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
        String domainName = mEtSearch.getText().toString().trim();
        loadData(domainName);
        mSuccess.setVisibility(View.VISIBLE);
        mSuccess02.setVisibility(View.GONE);
        mSuccess03.setVisibility(View.GONE);

    }

}
